/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */
package info.peigen.hotpot.component.gateway.core.mock;

import info.peigen.hotpot.common.core.result.ResultBase;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.core.result.ResultStatus;
import info.peigen.hotpot.common.service.base.gateway.AbstractGatewayRequest;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;

import java.io.Serializable;
import java.sql.Timestamp;

/**
 * <b>(ServiceContext)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2019/05/18
 */
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class MockServiceContext<REQUEST extends AbstractGatewayRequest, RESPONSE extends ResultBase> implements Serializable {
    static final long serialVersionUID = -7996169895294878828L;

    /** 当前数据库时间戳 <code>currentTimestamp</code> */
    Timestamp currentTimestamp;

    /** 服务耗时 */
    long elapsedTime;

    /**
     * 请求参数 <code>request</code>
     */
    private REQUEST request;

    /**
     * 应答返回 <code>response</code>
     */
    private RESPONSE response;

    public MockServiceContext(REQUEST request, RESPONSE response) {
        this.request = request;
        this.response = response;
    }

    public void setSuccess() {
        this.response.setStatus(ResultStatus.success);
        this.response.setCode(ResultCode.SUCCESS.code());
        this.response.setDetail(ResultCode.SUCCESS.message());
    }

    public void setFail() {
        this.response.setStatus(ResultStatus.failure);
        this.response.setCode(ResultCode.FAILURE.code());
        this.response.setDetail(ResultCode.FAILURE.message());
    }

    public void setProcessing() {
        this.response.markProcessing();
    }

    public void setFail(String code, String detail) {
        this.response.setStatus(ResultStatus.failure);
        this.response.setCode(code);
        this.response.setDetail(detail);
    }

    public void setFail(String detail) {
        this.response.setStatus(ResultStatus.failure);
        this.response.setCode(ResultCode.FAILURE.code());
        this.response.setDetail(detail);
    }

    public void setFailByCode(ResultCode code) {
        this.response.setStatus(ResultStatus.failure);
        this.response.setCode(code.code());
        this.response.setDetail(code.message());
    }

    public void setTimeout() {
        this.response.setStatus(ResultStatus.failure);
        this.response.setCode(ResultCode.CHANNEL_TIMEOUT.code());
        this.response.setDetail(ResultCode.CHANNEL_TIMEOUT.code());
    }
}